/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.PhanTrangDAO;
import model.dao.administratorDAO;
import model.pojo.adminTableColumnsPOJO;

/**
 *
 * @author Mr.Thanh
 */
public class adminSearch extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");        
        String table = request.getParameter("page");
        String field=request.getParameter("field");    
        String keyword=request.getParameter("keyword");
        try {
            if(table!=null){
                
                if(field==null && keyword==null){
                      ArrayList<adminTableColumnsPOJO> colsUpload=administratorDAO.getAllColumsFromTable("vndoan",table);
                       if(colsUpload.size()>0){                  

                                for(adminTableColumnsPOJO col:colsUpload){  
                                    String typ="";
                                    if(col.getType().indexOf("(")>0){
                                         typ=col.getType().substring(0,col.getType().indexOf("(")).trim();
                                    }else{
                                        typ=col.getType();
                                    }
                                     if(typ.startsWith("varchar")||
                                             typ.startsWith("nvarchar")||
                                             typ.startsWith("text")||
                                             typ.startsWith("ntext")){
                                         out.println ("<option value='"+typ+"'>"+col.getField()+"</option>");
                                     }
                                  }
                        }                    
                  
                }else{
                    int trang=1;
                    int soTrang=1;
                    if(request.getParameter("p")!=null){
                        trang=Integer.parseInt(request.getParameter("p"));
                    }
                    soTrang=PhanTrangDAO.tinhSoLuongTrang(table.trim());                                    
                    request.setAttribute("trang",trang);
                    request.setAttribute("soTrang", soTrang);
                        
                    if(field!=null){
                       
                       HashMap<Vector,ArrayList<adminTableColumnsPOJO>> ds=administratorDAO.SearchTablePage(table.trim(),trang,field,keyword);
                       request.setAttribute("DanhSach", ds);
                       request.getRequestDispatcher("/administrator/view.jsp").include(request, response);                  

                               
                    }
                }
              
             
            }
        } finally { 
            out.flush();
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
